package com.xiyucode.yuerclientsdk.client;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.xiyucode.yuerclientsdk.common.ApiMapping;
import com.xiyucode.yuerclientsdk.constantsUrl.ZhiPuAiConstantsUrl;
import com.xiyucode.yuerclientsdk.model.User;

import java.util.HashMap;
import java.util.Map;

import static com.xiyucode.yuerclientsdk.constantsUrl.EncryptedConstantsUrl.*;
import static com.xiyucode.yuerclientsdk.constantsUrl.NameConstantsUrl.*;
import static com.xiyucode.yuerclientsdk.constantsUrl.NumberConstantsUrl.GET_NUMBER_BY_GET;
import static com.xiyucode.yuerclientsdk.constantsUrl.ZhiPuAiConstantsUrl.ZHI_PU_AI_URL;
import static com.xiyucode.yuerclientsdk.utils.SignUtils.genSign;


/**
 * 调用第三方接口客户端
 *
 * author xiyu
 */

public class YuerApiClient {


    private String accessKey;

    private String secretKey;

    public YuerApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    @ApiMapping("/11")
    public String askZhiPuAi(String apiKey, String text) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("apiKey", apiKey);
        paramMap.put("text", text);
        String result = HttpRequest.post(ZHI_PU_AI_URL)
                .addHeaders(getHeaderMap(JSONUtil.toJsonStr(paramMap)))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    @ApiMapping("/7")
    public String getRandomNumberByGet(int begin, int end, int size) {
        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("begin", begin);
        paramMap.put("end", end);
        paramMap.put("size", size);
        String result = HttpRequest.get(GET_NUMBER_BY_GET)
                .addHeaders(getHeaderMap(JSONUtil.toJsonStr(paramMap)))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    @ApiMapping("/5")
    public String toEncryptedSalt(String text, String salt) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("text", text);
        paramMap.put("salt", salt);
        String result = HttpRequest.post(TO_ENCRYPTED_SALT)
                .addHeaders(getHeaderMap(text + salt))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    @ApiMapping("/4")
    public String toEncrypted(String text) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("text", text);
        String result = HttpRequest.post(TO_ENCRYPTED)
                .addHeaders(getHeaderMap(text))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }



    @ApiMapping("/1")
    public String getNameByGet(String name) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpRequest.get(GET_NAME_BY_GET)
                .addHeaders(getHeaderMap(name))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    @ApiMapping("/2")
    public String getNameByPost(String name) {
        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpRequest.post(GET_NAME_BY_POST)
                .addHeaders(getHeaderMap(name))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    private Map<String,String> getHeaderMap(String body){
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("accessKey", accessKey);
        // 不能在服务器端明文传递密钥
        // hashMap.put("secretKey",secretKey);
        hashMap.put("nonce", RandomUtil.randomNumbers(4));
        hashMap.put("body", body);
        hashMap.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
        hashMap.put("sign", genSign(body, secretKey));
        return hashMap;

    }

    @ApiMapping("/3")
    public String getUsernameByPost(User user) {
        String json = JSONUtil.toJsonStr(user);
        String result = HttpRequest.post(GET_NAME_BY_USER)
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    @ApiMapping("/6")
    public String superEncryptedSalt(User user, String text, String salt) {
        String json = JSONUtil.toJsonStr(user);

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("user", json);
        paramMap.put("text", text);
        paramMap.put("salt", salt);
        String result = HttpRequest.post(SUPPER_ENCRYPTED_SALT)
                .addHeaders(getHeaderMap(JSONUtil.toJsonStr(paramMap)))
                .form(paramMap)
                .execute()
                .body();
        System.out.println(result);
        return result;
    }

    @ApiMapping("/8")
    public String requestPythonToGetName(String name) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result = HttpUtil.get("localhost:8333/api/getUrlInfo", paramMap);
        System.out.println(result);
        return result;
    }

}